<?php
/*
	2010-5-20 14:50 ggg
	应用 API接口测试自检接口

*/


require_once "SelfCheckCase.php";
require_once dirname(__FILE__) . "/../../../util/site/require.php";
require_once dirname(__FILE__).'/../../../outer/API/SDOApi.php';



class SDOApiSelfCheck  extends SelfCheckCase
{

    protected static $_instance = null;
    private static $_app_id = "1000000100";
    private static $_user_id = 735434155;

	
	private $_app_secret	= null;

	private $_currMod	= null;

	private $_sysArray	= Array();

	//传应用 app id 
	function __construct() {

		$this->_currMod	= new SDOApi();

		$arr_params	= Array();

		$arr_params['appid'] = self::$_app_id;
		$app_info = OpAppModel::get(self::$_app_id);

		$this->_app_secret	 = $app_info['secret_key'];


		$arr_params['user'] = self::$_user_id;
		$arr_params['call_id'] = time();
		$arr_params['session_key'] = 
			OpAppAuth::getSessionKey($arr_params['user']
									 ,$app_info['system_secret_key']);

		$arr_params['api_key'] = $app_info['api_key'];
		$arr_params['format'] = 'json';
		$arr_params['v'] = 1;

		$this->_sysArray	 = $arr_params;
	}


    public static function getInstance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }


	function check_call($paramArray)
	{

		$all_paramArray	= $paramArray + $this->_sysArray;
		$all_paramArray['sig'] = SDOApi::createSign($all_paramArray,$this->_app_secret);

		$result = $this->_currMod->call($all_paramArray);
		$result = json_decode($result, true);
		if(isset($result['errorCode']))
		{
		    if ($result['errorCode'] === 900)
		    {
		        return  array(
                    'errorCode'     => 0,
                    'message'       => 'ok'
                );
		    }
			return $result;

		}

		//正常
        return  array(
            'errorCode'     => 0,
            'message'       => 'ok'
        );

	}

	//------ 测试用例
    static public function caseUsers_getLoggedInUser()
    {
		$curr_obj	= self::getInstance();
		$result	= $curr_obj->check_call(Array('method' => 'Users.getLoggedInUser'));

        return self::reportError($result['errorCode'], $result['message']);

	}

    static public function caseUsers_getInfo()
    {
		$curr_obj	= self::getInstance();
		$result	= $curr_obj->check_call(Array('method' => 'Users.getInfo','fields'=>'id'));

        return self::reportError($result['errorCode'], $result['message']);

	}



    static public function caseFriends_get()
    {
		$curr_obj	= self::getInstance();
		$result	= $curr_obj->check_call(Array('method' => 'Friends.get'));

        return self::reportError($result['errorCode'], $result['message']);

	}


    static public function caseFriends_getFriends ()
    {
		$curr_obj	= self::getInstance();
		$result	= $curr_obj->check_call(Array('method' => 'Friends.getFriends'));

        return self::reportError($result['errorCode'], $result['message']);

	}


    static public function caseFriends_getAppFriends()
    {
		$curr_obj	= self::getInstance();
		$result	= $curr_obj->check_call(Array('method' => 'Friends.getAppFriends'));

        return self::reportError($result['errorCode'], $result['message']);

	}
}


